package com.tuiyun.jxgl.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.transaction.annotation.Transactional;

import com.tuiyun.jxgl.bean.Driver;
import com.tuiyun.jxgl.bean.Student;
import com.tuiyun.jxgl.bean.Stuselectdriver;
import com.tuiyun.jxgl.dao.DriverHome;
import com.tuiyun.jxgl.dao.StuselectdriverHome;

public class ListDriverAction {
	
	private DriverHome dh;
	private StuselectdriverHome ssh;
	
	private String msg;
	private int msg_type;
	
	private String q;
	private byte region = 0;
	private int total;
	private int page_size;
	private int page_index;
	private int page_count;
	private List<Driver> lds;
	private int driverId;
	@Transactional
	public String execute(){		
		byte needType = -1;
		Subject currentUser = SecurityUtils.getSubject();
		if(currentUser.isAuthenticated()){
			Object o = currentUser.getPrincipal();
			if(o instanceof Student){
				Student student = (Student) o;
				if(!student.getName().equalsIgnoreCase("admin")){
					needType = student.getCarType();
					Stuselectdriver ssd = ssh.findByStudent(student.getId());
					if(ssd == null){
						msg_type = 1;
						msg = "您当前还未选择教练";
					}else{
						msg_type = 2;
						msg = "您当前已经选择教练"+ssd.getDriver().getRealName()+"，请等待教练联系您，若未联系请致电询问";
						driverId = ssd.getDriver().getId();
					}
				}else{
					msg_type = 3; // means admin
				}
			}			
		}
		page_size = 10;
		if(q != null && q.length() >= 0){
//			try {
//				q = URLDecoder.decode(q,"UTF-8");
//			} catch (UnsupportedEncodingException e) { 
//				e.printStackTrace();
//			}
			total = dh.findCountLikeName(q,region,needType);
			setPage_count((int) Math.floor(total*1.0/page_size + 0.5));
			if(page_index <= 0)
				page_index = 1;
			if(page_index >= page_count)
				page_index = page_count;
			lds = dh.findByRangeLikeName(page_size, page_index,q,region,needType);
		}else{
			total = dh.findCount(region,needType);
			setPage_count((int) Math.floor(total*1.0/page_size + 0.5));
			if(page_index <= 0)
				page_index = 1;
			if(page_index >= page_count)
				page_index = page_count;
			lds = dh.findByRange(page_size, page_index,region,needType);
		}		
		return "success";
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public int getPage_size() {
		return page_size;
	}
	public void setPage_size(int page_size) {
		this.page_size = page_size;
	}
	public int getPage_index() {
		return page_index;
	}
	public void setPage_index(int page_index) {
		this.page_index = page_index;
	}
	public DriverHome getDh() {
		return dh;
	}
	public void setDh(DriverHome dh) {
		this.dh = dh;
	}
	public List<Driver> getLds() {
		return lds;
	}
	public void setLds(List<Driver> lds) {
		this.lds = lds;
	}
	public int getPage_count() {
		return page_count;
	}
	public void setPage_count(int page_count) {
		this.page_count = page_count;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public int getMsg_type() {
		return msg_type;
	}
	public void setMsg_type(int msg_type) {
		this.msg_type = msg_type;
	}
	public StuselectdriverHome getSsh() {
		return ssh;
	}
	public void setSsh(StuselectdriverHome ssh) {
		this.ssh = ssh;
	}
	public int getDriverId() {
		return driverId;
	}
	public void setDriverId(int driverId) {
		this.driverId = driverId;
	}
	public String getQ() {
		return q;
	}
	public void setQ(String q) {
		this.q = q;
	}
	public byte getRegion() {
		return region;
	}
	public void setRegion(byte region) {
		this.region = region;
	}	
	
}
